// 制冷站 - 工艺能效--头顶6大块 scripts/codeUsedScript/1909.js
dev.action = function (resp, param) {

    if (param.time == undefined) {
        resp.setJsData("请传参数time");
        return;
    }

    if (param.id == undefined) {
        resp.setJsData("请传参数设备ID");
        return;
    }

    var timeFormatMap = {
        'year': "%Y",
        'month': "%Y-%m",
        'day': "%Y-%m-%d",
        'hour': "%Y-%m-%d %H"
    };
    var timeFlag = '';
    var names = ['总冷量','总用水','总耗电','总电费','单方用电','单方电费']
    var units = ['m³','m³','kwh','元','kwh/m³','元/m']
    switch (param.time.length) {
        case 4:
            timeFlag = 'year'
            break;
        case 7:
            timeFlag = 'month'
            break;
        case 10:
            timeFlag = 'day'
            break;
        case 13:
            timeFlag = 'hour'
            break;
        default:
            break;
    }
    if (timeFlag == '') {
        resp.setJsData('检查下为何时间格式为空退出的...');
        return;
    }

    var sql ="select data_type as type, sum(dvalue) as useNum, date_format(the_time, '"+ timeFormatMap[timeFlag] +"') as time\n" +
        " from iot_dev_data_win\n" +
        " where dev_id = " + param.id +
        " and date_format(the_time, '"+ timeFormatMap[timeFlag] +"') = '" + param.time +"'\n" +
        " group by data_type";

    var db = iot.db();

    var resultValue = db.qry(sql);

    if (JSON.stringify(resultValue.value) == '{}'){

    }
    var result = iot.list()
    for (var key in resultValue.value) {
        result.add(resultValue.value[key])
    }
    var backk = iot.list()
    for (var i=0;i<names.length;i++){
        var cur = names[i]
        var mp = {}
        mp.type = cur
        mp.unit = units[i]
        for (var j =0;j<result.length;j++) {
            var jCur = result[j]
            if (jCur.type == cur) {
                mp.useNum = jCur.useNum
                break;
            }
        }

        if (mp.useNum == undefined) {
            mp.useNum = '--'
        }
        backk.add(mp)
    }

    resp.setJsData(backk);
}
